package com.example.xmpic.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.example.common.utils.DisplayUtils;
import com.example.xmpic.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author skygge
 * @date 2020-06-05.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：横向滑动选择时间控件
 */
public class HistoryView extends View {

    private Context context;
    private Paint paint;
    private int viewHeight;
    private int margin_left_color;
    private int normal_video_color;
    private int sense_video_color;
    private int empty_video_color;

    private static final float TOTAL_PROCESS_HOUR = 4320f;
    private static final float TOTAL_PROCESS_MINUTE = 25920f;
    private final static int ALARM =  0;
    private final static int NORMAL = 1;
    private final static int mWeekSize = 12;
    private final static int mMarginLeft = 14;
    private int mMarginBottom = 3;

    private boolean isHourOrMinute;

    private List<Map<String,Object>> mList;

    public HistoryView(Context context) {
        super(context);
        initView(context);
    }

    public HistoryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HistoryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setEmptyVideoColor(int empty_color) {
        this.empty_video_color = empty_color;
    }

    public void setNormalVideoColor(int normal_color) {
        this.normal_video_color = normal_color;
    }

    public void setSenseVideoColor(int sense_color) {
        this.sense_video_color = sense_color;
    }

    private void initView(Context context){
        this.context = context;
        paint = new Paint();
        paint.setAntiAlias(true);
        mList = new ArrayList<>();
        this.margin_left_color = ContextCompat.getColor(context, R.color.empty_history);
        this.mMarginBottom = DisplayUtils.dip2px(context, 3);
        setEmptyVideoColor(ContextCompat.getColor(context, R.color.empty_history));
        setNormalVideoColor(ContextCompat.getColor(context, R.color.common_history));
        setSenseVideoColor(ContextCompat.getColor(context, R.color.detection_history));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(empty_video_color);
        canvas.drawRect(0, 0, isHourOrMinute ? TOTAL_PROCESS_HOUR : TOTAL_PROCESS_MINUTE, viewHeight, paint);//绘制矩形

        int margin = (int)(mMarginLeft * getResources().getDisplayMetrics().scaledDensity);
        paint.setColor(margin_left_color);
        canvas.drawRect(0, 0, margin, viewHeight, paint);//绘制填充

        for(int i = 0;i<mList.size();i++){
            int beginTime = (int) mList.get(i).get("start");
            int endTime   = (int) mList.get(i).get("end");
            float start = (isHourOrMinute ? (beginTime/20f) : (beginTime * 3f/10f)) + margin;
            float end = (isHourOrMinute ? (endTime/20f) : (endTime * 3f/10f)) + margin;
            switch ((int)mList.get(i).get("type")){ //设置画笔颜色
                case NORMAL:
                    paint.setColor(normal_video_color);
                    canvas.drawRect(start, 0, end, 3*viewHeight/4f, paint);
                    break;
                case ALARM:
                    paint.setColor(sense_video_color);
                    canvas.drawRect(start, 0, end, 3*viewHeight/4f, paint);
                    break;
                default:
                    paint.setColor(empty_video_color);
                    canvas.drawRect(start, 0, end, 3*viewHeight/4f, paint);
                    break;
            }
        }

        paint.setColor(ContextCompat.getColor(context, R.color.line_history));

        if (isHourOrMinute){
            for(int i = 0; i<25; i++){
                paint.setStrokeWidth(4);
                canvas.drawLine(i*TOTAL_PROCESS_HOUR/24 + margin,viewHeight/4f,i*TOTAL_PROCESS_HOUR/24 + margin,3*viewHeight/4f,paint);
            }

            for(int i = 0; i<72; i++){
                if(i%3!=0){
                    paint.setStrokeWidth(2);
                    canvas.drawLine(i*TOTAL_PROCESS_HOUR/72 + margin,viewHeight/3f,i*TOTAL_PROCESS_HOUR/72 + margin,2*viewHeight/3f,paint);
                }
            }
        }else {
            for(int i = 0; i<145; i++){
                paint.setStrokeWidth(4);
                canvas.drawLine(i*TOTAL_PROCESS_MINUTE/144 + margin,viewHeight/4f,i*TOTAL_PROCESS_MINUTE/144 + margin,3*viewHeight/4f, paint);
            }
        }
        paint.setStyle(Paint.Style.FILL);
        float size = mWeekSize * getResources().getDisplayMetrics().scaledDensity;
        paint.setTextSize(size);
        if (isHourOrMinute){
            for(int i=0; i < 24; i++){
                String text = i<10?("0"+i+":00"):(i+":00");
                int startX = (int)TOTAL_PROCESS_HOUR/24 * i;
                int startY = viewHeight - mMarginBottom;
                canvas.drawText(text, startX, startY, paint);
            }
        }else {
            int mCount = 0;
            for(int i=0; i < 24; i++){
                for (int j = 0; j < 6; j++) {
                    String hour = i<10?("0"+i):(i+"");
                    String minute = j+"0";
                    String time = hour + ":" + minute;
                    int startX = (int)TOTAL_PROCESS_MINUTE/144 * mCount;
                    int startY = viewHeight - mMarginBottom;
                    canvas.drawText(time, startX, startY, paint);
                    mCount++;
                }
            }
        }
        canvas.save();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if(heightMode == MeasureSpec.EXACTLY){
            viewHeight = heightSize;
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    public void setPlayList(List<Map<String,Object>> list, boolean isHour){
        this.mList = list;
        this.isHourOrMinute = isHour;
        invalidate();
    }
}
